package com.obtk.controllerSW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.CstActivity;
import com.obtk.service.CstActivityService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-06 14:06:41
 */
@Controller
@RequestMapping("/cstActivity")
public class CstActivityController {
    @Autowired
    private CstActivityService cstActivityService;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(CstActivity cstActivity, Integer limit, Integer offset, Model model){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<CstActivity> cstActivitiesList = cstActivityService.selectCstActivity(cstActivity);
        PageInfo<CstActivity> pages = new PageInfo<>(cstActivitiesList);
        System.out.println(cstActivitiesList);
        return ResultUtil.table(cstActivitiesList,pages.getTotal());
    }
    /**新增用户*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(CstActivity cstActivityForm){
        int num = cstActivityService.register(cstActivityForm);
        if(num > 0){
            return ResultUtil.success("添加记录成功");
        }else {
            return ResultUtil.error("添加记录失败");
        }
    }


    /**编辑用户详情*/
    @GetMapping("/edit")
    public String userDetail(Model model, Integer avtId){
        CstActivity cstActivity = cstActivityService.selectByAvtId(avtId);
        System.out.println("controller"+avtId);
        model.addAttribute("cstActivity", cstActivity);
        return "cstActivity/update";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(CstActivity cstActivityForm){
        int a = cstActivityService.updateByCstActivityId(cstActivityForm);
        if (a > 0) {
            return ResultUtil.success("编辑记录成功！");
        } else {
            return ResultUtil.error("编辑记录失败");
        }
    }
    /**删除商品*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String avtId) {
        List<String> lkmIdsList = Arrays.asList(avtId);
        int a = cstActivityService.updateStatusBatch(lkmIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除记录成功");
        } else {
            return ResultUtil.error("删除记录失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String avtIdStr) {
        String[] avtIds = avtIdStr.split(",");
        List<String> cstOrderIdsList = Arrays.asList(avtIds);
        int a = cstActivityService.updateStatusBatch(cstOrderIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除记录成功");
        } else {
            return ResultUtil.error("删除记录失败");
        }
    }
}
